package com.example.demo.config;

import com.example.demo.component.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-07
 * Time: 8:57
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Value("${imgpath}")
    private String imagePath;

    /**
     *  实现映射外部图片路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 当访问 /image/** 时, 会映射到我的本地路径 imagePath
        registry.addResourceHandler("/image/**").addResourceLocations("file:" +imagePath);
    }

    @Autowired
    private LoginInterceptor loginInterceptor;

    // 重写添加拦截器并配置规则

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") // 拦截所有类
                //.excludePathPatterns("/login.html")
                .excludePathPatterns("/login3.html")
                .excludePathPatterns("/reg2.html")
                //.excludePathPatterns("/reg.html")
                .excludePathPatterns("/blog_list.html") // 所有人能看到的文章页 - 灌水区
                .excludePathPatterns("/blog_content.html") // 所有人能看到的文章中可以点击看见详情
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/image/**")
                .excludePathPatterns("/editor.md/**")
                .excludePathPatterns("/js/**")
                //.excludePathPatterns("/user/reg") // 注册接口
                .excludePathPatterns("/user/reg2")
                .excludePathPatterns("/user/login") // 登陆接口
                .excludePathPatterns("/article/getdetial")
                .excludePathPatterns("/article/addvisits")
                .excludePathPatterns("/article/countart")
                .excludePathPatterns("/user/islogin")
                .excludePathPatterns("/article/getAllArticleByPage")
                .excludePathPatterns("/article/allarticle")
                .excludePathPatterns("/user/email")
                .excludePathPatterns("/user/getcode")
                .excludePathPatterns("/forgot.html")
                .excludePathPatterns("/user/forgot")
                .excludePathPatterns("/user/upload")
                //.excludePathPatterns("/personal_center.html")
                .excludePathPatterns("/article/initimage")
                .excludePathPatterns("/image/pric")
        ;

    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof StringHttpMessageConverter);
    }
}
